﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RSPJ.Struct;
using RSPJ.Windows;
using RSPJ.Function;

namespace RSPJ.Windows
{
    public partial class BandMath : Form
    {
        char[] expc;//表达式数组

        int lines, samples;

        List<double[,]> bandlist = new List<double[,]>();//bandlist
        Files outFile = new Files();
        int flag = 0;

        public BandMath()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string exp = txtExpression.Text;
            outFile.Name = "Result(" + exp + ")";
            if (exp == "")
                MessageBox.Show("请输入表达式！");
            else
            {
                exp += "#          ";
                expc = exp.ToCharArray();

                //选择波段
                for (int i = 0; i < expc.Length; i++)
                {
                    if (expc[i] == 'b')
                    {
                        int index = bandlist.Count + 1;
                        using (SelectBands sb = new SelectBands("选择b" + index + "波段", 1))
                        {
                            if (sb.ShowDialog() == DialogResult.OK)
                            {
                                for (int j = 0; j < GlobalData.fileList.Count; j++)
                                {
                                    if (sb.FileBand == GlobalData.fileList[j].Name)
                                    {
                                        if (flag == 0)
                                        {
                                            lines = GlobalData.fileList[j].Head.Lines;
                                            samples = GlobalData.fileList[j].Head.Samples;
                                            flag = 1;
                                        }
                                        else if (lines != GlobalData.fileList[j].Head.Lines || samples != GlobalData.fileList[j].Head.Samples)
                                        {
                                            MessageBox.Show("选择的波段行列数不相同！");
                                            i--;
                                            break;
                                        }
                                        double[,] band = new double[lines, samples];
                                        for (int p = 0; p < lines; p++)
                                            for (int q = 0; q < samples; q++)
                                                band[p, q] = GlobalData.fileList[j].IntArray[sb.Band - 1, p, q];
                                        bandlist.Add(band);
                                    }
                                }
                            }
                        }
                    }
                }

                outFile.Head.Lines = lines;
                outFile.Head.Samples = samples;
                outFile.IntArray = new double[1, lines, samples];

                funcBandMath fbm = new funcBandMath(bandlist, lines, samples);
                double[,] tmp = fbm.Expression(expc);
                for (int p = 0; p < lines; p++)
                    for (int q = 0; q < samples; q++)
                        outFile.IntArray[0, p, q] = tmp[p, q];

                using (ConvertData cd = new ConvertData(outFile))
                {
                    if (cd.ShowDialog() == DialogResult.OK)
                    {
                        GlobalData.fileList.Add(cd.file);
                        // 打开波段列表
                        if (WindowsStatus.availableBandsList == false)
                        {
                            AvailableBandsList bandsWindow = new AvailableBandsList();
                            WindowsStatus.availableBandsList = true;
                            bandsWindow.Show();
                        }
                    }
                }
                DialogResult = DialogResult.OK;
                Close();
            }
        }

        private void BandMath_Shown(object sender, EventArgs e)
        {
            outFile.Head.Bands = 1;
            outFile.Head.Data_type = 5;
            outFile.Head.File_type = "ENVI Standard";
            outFile.Head.Header_offset = 0;
            outFile.Head.Interleave = "bsq";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
